#include "Rp2VisibleSet.h"

using namespace Rp2;

//---------------------------------------------------------------------------------------------------
VisibleSet::VisibleSet (int iMaxQuantity, int iGrowBy)
{
    m_akVisible = 0;
    Resize(iMaxQuantity,iGrowBy);
}
//---------------------------------------------------------------------------------------------------
VisibleSet::~VisibleSet()
{
	delete[] m_akVisible;
}
//---------------------------------------------------------------------------------------------------
void VisibleSet::Insert (Spatial* pkObject, Effect* pkGlobalEffect)
{
    // Increase the array storage if necessary.
    if (m_iQuantity >= m_iMaxQuantity)
    {
        int iNewMaxQuantity = m_iMaxQuantity + m_iGrowBy;
        VisibleObject* akNewVisible = new VisibleObject[iNewMaxQuantity];
        size_t uiSize = m_iQuantity*sizeof(VisibleObject);
        System::Memcpy(akNewVisible,uiSize,m_akVisible,uiSize);
        delete[] m_akVisible;
        m_akVisible = akNewVisible;
        m_iMaxQuantity = iNewMaxQuantity;
    }

    // Append the potentially visible object to the set.
    int iIndex = m_iQuantity++;
    m_akVisible[iIndex].Object = pkObject;
    m_akVisible[iIndex].GlobalEffect = pkGlobalEffect;
}
//---------------------------------------------------------------------------------------------------
void VisibleSet::Resize(int iMaxQuantity, int iGrowBy)
{
    if (iMaxQuantity > 0)
    {
        m_iMaxQuantity = iMaxQuantity;
    }
    else
    {
        m_iMaxQuantity = VS_DEFAULT_MAX_QUANTITY;
    }

    if (iGrowBy > 0)
    {
        m_iGrowBy = iGrowBy;
    }
    else
    {
        m_iGrowBy = VS_DEFAULT_GROWBY;
    }

    delete[] m_akVisible;
    m_iQuantity = 0;
    m_akVisible = new VisibleObject[m_iMaxQuantity];
}
//---------------------------------------------------------------------------------------------------